<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    LineData: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    LineData: {
      deep: true,
      handler(val) {
        this.setOptions()
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions()
      // this.chart.setOption(options)
    },
    setOptions() {
      const data1 = this.LineData.map((item) => {
        return item.name
      })
      const data2 = this.LineData.map((item) => {
        return Number(item.value).toFixed(2)
      })
      this.chart.setOption({
        // backgroundColor:'rgb(0,0,0,0)',
        grid: { left: '1%', right: '1%', top: '2%', bottom: 0, containLabel: true },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            lineStyle: {
              width: 1,
              color: '#019680'
            }
          }
        },

        xAxis: {
          splitLine: { show: false }, // 去除网格线
          splitArea: { show: false }, // 保留网格区域
          axisLabel: {
            formatter: '{value} '
          },
          type: 'category',
          boundaryGap: false,
          data: data1
        },
        yAxis: {
          type: 'value',
          splitLine: { // 网格线
            show: false, // 关闭网格线
            // 或者
            lineStyle: {
              type: 'solid', // 设置网格线类型 dotted：虚线   solid:实线
              color: '#0D56EF', // 网格线颜色
              backgroundColor: '#000'
            }
          },
          splitArea: { show: true }

        },
        series: [
          {
            data: data2,
            tooltip: {
              valueFormatter: function(value) {
                return value + 'KG'
              }
            },
            type: 'line',
            smooth: false, // 折现为曲线会=或者直线
            areaStyle: { // 区域填充渐变颜色
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [{
                  offset: 0, color: 'rgba(0,249,223,1)' // 0% 处的颜色
                }, {
                  offset: 1, color: 'rgba(0,135,185,0.11)' // 100% 处的颜色
                }],
                global: false // 缺省为 false
              }
            }
          }
        ]

      })
    }
  }
}
</script>
